﻿///For IntelliSense purpose
///<reference path="/scripts/jquery-1.6.4.js" />
///<reference path="/scripts/jquery-ui.min.js

/***************** Page loading process *****************\
1. Load first and second page
2. Click peek
3. Show second page
4. Update content of first page (including header and body)
5. 

\********************************************************/
//global constants definitions
//
//pivot constants
bodyWidth = 1000;
pvtWidth = 880;
peekWidth = 120;
animationSpeed = 700;
numOfTeams = 0;

//team-members accordion constants
maxWidth = 210;
minWidth = 75;

//current counter and team index 
counter = -1;
teamIndex = 0;

//
isExpired = true;

$('document').ready(function () {
    //Set the first of the navigation as current
    $('.top .navigation ul li').eq(0).addClass('current');
    //Get num of teams
    numOfTeams = $('.top .navigation ul li').size();
    //Init sliders
    $.proxy(InitReportSlider(0), this);
    $.proxy(InitTeamMembers(), this);

    FetchDashboard();

    //Initialize navigation
    $('.top .navigation ul li').click(function (target) {
        //Check if current one clicked;
        if ($(this).hasClass('current')) {
            return;
        }
        //Check if animation in progress
        if ($('.inner').is(':animated')) {
            return;
        }

        ReportsToggle(false);
        DashboardToggle(false);
        //Retrieve next team index
        teamIndex = $(this).index();
        //Update navigation selection;
        UpdateTeamIndex(teamIndex);
        //Increase counter
        counter++;
        Next();
    });

    //Initialze page sliding
    $('.peek').live('click', function () {
        //Check if animation in progress
        if ($('.inner').is(':animated')) {
            return;
        }

        ReportsToggle(false);
        DashboardToggle(false);
        UpdateTeamIndex();
        counter++;
        Next();
    });

    $("#dialog").dialog({
        autoOpen: false,
        show: "blind",
        hide: "blind",
        modal: "true",
        minWidth: 500,
        minHeight: 300
    });

    $('.block .content li a').live('click', function (sender, args) {
        $('#dialog h1').text($(sender.target).text());
        FetchNews($(sender.target).attr('class'), $(sender.target).attr('href'));
        $('#dialog').dialog('open');
        return false;
    });

    $find("ReportViewer0").add_propertyChanged(ShowDashboard);
    $find("ReportViewer1").add_propertyChanged(ShowDashboard);


    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(OnUpdateReady);

});


function ShowDashboard(sender) {
    return;
}

function OnUpdateReady(sender, args) {
    try{
        if (isExpired && args.get_panelsUpdated().length > 2) {        
            var i = (counter + 1) % 2;
            $.proxy(InitReportSlider(i), this);
            $.proxy(InitTeamMembers(), this);
            isExpired = false;
            ReportsToggle(true);
//            DashboardToggle(true);
            FetchDashboard();
        }
    }
    catch(e){}
}

function InitReportSlider(i) {
    //Initialize report slider
    $('.top-reports .content').eq(i).slides({
        generateNextPrev: false,
        play: 0,
        pagination: true,
        generatePagination: false,
        container: 'report-slider',
        slideSpeed: 1200,
        autoHeight: true,
        autoHeightSpeed: 350,
        next: 'next',
        prev: 'prev'
    });
}

function InitTeamMembers() {
    //Initilize team members 
    lastBlock = $("#a1");
    $(".team-members ul li a").hover(
        function () {
            $(lastBlock).animate({ width: minWidth + "px" }, { queue: false, duration: 400 });
            $(this).animate({ width: maxWidth + "px" }, { queue: false, duration: 400 });
            lastBlock = this;
        }
    );
}

function UpdateTeamIndex(i) {
    if (i >= 0) {
        teamIndex = i;
    }
    else {
        teamIndex++;
    }
    teamIndex %= numOfTeams;
}

function UpdateNav(i) {
    $('.top .navigation ul li.current').removeClass('current');
    $('.top .navigation ul li').eq(i).addClass('current');
}

function Next() {
    isExpired = true;
    UpdateNav(teamIndex);
    //Fetch content of current team
    FetchContent();
    //Move header
    NextHeader();
    //Move body
    NextBody();
    //Switch peek block
    $('.pvtHeader .inner').eq((counter + 1) % 3).children('a').removeClass('peek');
    $('.pvtHeader .inner').eq((counter + 2) % 3).children('a').addClass('peek');
    //Update background color of next header block
    setTimeout(
            function () {
                var o = $('.pvtHeader .inner').eq((counter) % 3);
                if (o.hasClass('blue')) {
                    o.removeClass('blue');
                    o.addClass('orange');
                }
                else {
                    o.removeClass('orange');
                    o.addClass('blue');
                }
            },
            animationSpeed - 10
    );
}

function NextHeader() {
    //Move container
    $('.pvtHeader .inner').animate(
        { left: '-='+pvtWidth },
        {
            duration: animationSpeed,
            complete: function () {
                $('.pvtHeader .inner').eq(counter % 3).css('left', 2*pvtWidth);
            }
        }
    );

    //Move text
        $('.inner .headline').eq((counter + 1) % 3).animate(
        { left: pvtWidth - 100 },
        {
            duration: animationSpeed,
            complete: function () {
                $('.inner .headline').eq(counter%3).css('left', 0);
            }
        }
    );
}

function NextBody() {
    $('.pvtBody .inner').animate(
        { left: '-=1000' },
        {
            duration: animationSpeed,
            complete: function () {
                $('.pvtBody .inner').eq(counter % 2).css('left', bodyWidth);
            }
        }
    );
}

function FetchContent() {        
    document.getElementById("counter").value = counter;
    document.getElementById("teamIndex").value = teamIndex;
    __doPostBack('trigger','');
}

function FetchDashboard() {
    __doPostBack('trigger2', '');
}

function FetchNews(type, id) {
    document.getElementById("newsType").value = type;
    document.getElementById("newsID").value = id;
    __doPostBack('trigger3', '');
}


//true for display, false for hide
function ReportsToggle(b){
    if (b) {
        $('div.report>*').fadeIn(300);
    }
    else {
        $('div.report>*').hide();
    }
}

function DashboardToggle(b) {
    if (b) {
        $('div.dashboard *').show();
    }
    else {
        $('div.dashboard *').hide();
    }
}